*********************
* Appendix Exhibits *
*********************


* Figure A2: Goal-setting Strategies

* Table A1: Predictors of Participation in Interventions
* Table A2: Contraceptive Take up Rates By Treatment Status and Over Time
* Table A4: Treatment-Control Balance At Baseline
* Table A5: External Validity
* Table A6: Attririon: Female Sample
* Table A7: Attirion: Boys Sample
* Table A8: Impact of Treatment, No Controls
* Table A9: Impact of Treatment, Individual Terms
* Table D1: Endline Characteristics of Females, By status of Baseline Partnership


#delimit;

clear;
clear matrix;
estimates clear;
eststo clear;
set more off;
cap log close;

cd "$directory";


global controls_bl = "hghst_grade_bl own_house_bl momtalk_sex_never_bl age_bl ";
global ipv3b = "ipv_often ipv_12mo";


**************************************************
* Figure A2: Goal-setting Strategies
**************************************************


#d;
use "data/dataforfigureA2.dta", clear;

#delimit cr
*We need to create one variable that contains all the means:
collapse gs_abstinence_st gs_faithfulness_st gs_testing_st gs_condom_use_st gs_sexual_background_st gs_checkup_health_educ_st gs_practice_safe_sex_st gs_no_alcohol_drug_st gs_no_sharing_sharp_st gs_no_porn_st gs_no_bad_peer_st gs_blood_transfusion_st gs_good_ethics_behavior_st gs_school_st gs_partner_health_educ_st gs_reduce_nbpartner_st

*To reshape:
rename gs_abstinence_st T1
rename gs_faithfulness_st T2
rename gs_testing_st T3
rename gs_condom_use_st T4
rename gs_sexual_background_st T5
rename gs_checkup_health_educ_st T6
rename gs_practice_safe_sex_st T7
rename gs_no_alcohol_drug_st T8
rename gs_no_sharing_sharp_st T9
rename gs_no_porn_st T10
rename gs_no_bad_peer_st T11
rename gs_blood_transfusion_st T12
rename gs_good_ethics_behavior_st T13
rename gs_school_st T14
rename gs_partner_health_educ_st T15
rename gs_reduce_nbpartner_st T16

gen id=_n
reshape long T, i(id) j(type)
drop id

*Rename variables
rename T mean

tostring type , gen(Type)
drop type

replace Type="Abstinence" if Type=="1"
replace Type="Be Faithful" if Type=="2"
replace Type="HIV/STI Testing" if Type=="3"
replace Type="Use Condom" if Type=="4"
replace Type="Discuss Sexual History" if Type=="5"
replace Type="Health Checks" if Type=="6"
replace Type="Practice Safe Sex" if Type=="7"
replace Type="No Alcohol/drugs" if Type=="8"
replace Type="No sharing sharp objects" if Type=="9"
replace Type="No Porn" if Type=="10"
replace Type="Avoid Bad Peers" if Type=="11"
replace Type="No Blood Transfusion" if Type=="12"
replace Type="Good Ethics" if Type=="13"
replace Type="Focus on School" if Type=="14"
replace Type="Educate Partner" if Type=="15"
replace Type="Fewer Sex Partners" if Type=="16"

*Bar figure:
gen mean100= mean*100   /*To show as percent*/


graph bar mean100 , over(Type, sort(1) descending lab(angle(45))) graphregion(fcolor(gs16)) bar(1, fcolor(gs11)) bar(1, lcolor(gs11))   ytitle("Share of Respondents %", size()) title("", size(large) color(black)) blabel(bar, format(%12.1f) size(vsmall)) ylabel(0(10)60, grid gmax gmin angle(0) glcolor(gs15) glwidth(vthin) labsize(small)) 

*to add the %s
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph


graph export "output/Figure_A2.pdf", replace





**************************************************
* Table A1: Predictors of Participation in Interventions
**************************************************


#delimit;
use "data/boys-replication-data.dta", clear;

global controls_boy = " enrolled_bl age_bl own_house_bl dadtalk_sex_never_bl hhm_number_bl disc_factor_bl risk_tol_ef_bl i.region m_enrolled"; 
/* global controls_boy = " enrolled age own_house dadtalk_sex_never hhm_number disc_factor risk_tol_ef i.region m_enrolled";  */

foreach x in enrolled_bl {;

gen m_`x' = `x'==.;
replace `x'=0 if `x'==.;

};

keep if endline==0 & treat==3;


foreach out in grs_contact grs_enroll {;
	
	reg `out' $controls_boy, cluster(girlid);
	estimates store `out';
	sum `out';
	estadd scalar m = r(mean);
};

#delimit;
esttab grs_contact grs_enroll using "output/Table_A1.tex",
title("Predictors of enrolling in GRS" ) star(* 0.10 ** 0.05 *** 0.01)
collabels(none)  cells(b(star fmt(3)) se(par fmt(3))  p(par([ ]) fmt(3))) 
keep(enrolled_bl age_bl own_house_bl dadtalk_sex_never_bl hhm_number_bl disc_factor_bl risk_tol_ef_bl)
label style(tab) noconstant replace stats(m N, fmt(3 0) label("Outcome mean" "Observations"));
eststo clear;

#d;
use "data/girls-replication-data.dta", clear;
*Predictors;
global a = "enrolled_bl age_bl own_house_bl momtalk_sex_never_bl hhm_number_bl disc_factor_bl risk_tol_ef_bl phq2_ih_bl self_eff_std";


global fe = "branch";

*Outcome;
global ga = "strategy_num achieve_strat";

keep if  treat_goal==1;
local i=0;
foreach ga in $ga {;

foreach x in  a {;
areg `ga' $`x'  if endline==0, cluster(clubs) abs($fe);
sum `ga' if endline==0;
estadd scalar m = r(mean);
local i=`i'+1;
estimates store est`i';
};

local j=`j'+1;


/* estimates clear;
eststo clear; */
};

esttab est* using "output/Table_A1.tex",
title("Number of Strategies Achieved" ) star(* 0.10 ** 0.05 *** 0.01)
collabels(none)  cells(b(star fmt(3)) se(par fmt(3))) 
label style(tab) noconstant append stats(m N, fmt(3 0) label("Outcome mean" "Observations"));



**************************************************
* Table A2: Contraceptive Take up Rates By Treatment Status and Over Time
**************************************************

#d;
use "data/girls-replication-data.dta", clear;

keep if (control_group==1|treat_supply==1) & panel_bal==2;

collect clear;

collect: table endline treat_supply if panel_bal==2&treat_boys==0, stat(mean useinj1 useimp1 useiud1 usepill1) nformat(%9.3f) nototals;

collect layout (var#endline) (treat_supply);
collect export "output/Table_A2.tex", replace;

foreach outcome in useinj1 useimp1 useiud1 usepill1 {;
    areg `outcome' treat_supply##endline if panel_bal==2&treat_boys==0, cluster(clubs) abs(branch);
    eststo `outcome';
    
     
};

esttab useinj1 useimp1 useiud1 usepill1 using "output/Table_A2.tex", keep(1.treat_supply#1.endline) stats(N, fmt(0)) se nostar append;


**************************************************
* Table A4: Treatment-Control Balance At Baseline
**************************************************

#delimit;

estimates clear;
eststo clear; 

global ipv3b ipv_often ipv_12mo;					

global balance_outcomes ipv_often_bl ipv_12mo_bl 
                        had_sex_bl has_partner_bl partner_past2yr_bl total_partners_bl 
                        momtalk_sex_never_bl age_bl hghst_grade_bl married_coh own_house_bl;


use "data/girls-replication-data.dta", clear;
keep if endline==0&panel_bal==2;

				  
foreach out in $balance_outcomes {;
	
	
reg `out' treat_boys treat_supply i.region 
		if endline==0&panel_bal==2, cluster(clubs);
		
sum `out' if endline==0&control_group==1&panel_bal==2;
estadd scalar outmean1 = r(mean);

estimates store `out';
	
};


matrix C1=r(coefs);
matrix S1=r(stats);



#d;
use "data/girls-replication-data.dta", clear;
keep if panel_bal==2&had_sex_bl==1&partner_past2yr_bl==1;


#d;
global outcomes ipv_often ipv_12mo;

				  	
#d;			
foreach out in $outcomes {;
	
    reg `out' treat_boys treat_supply i.region 
            if endline==0, cluster(clubs);
            
    sum `out' if endline==0&control_group==1;
    estadd scalar outmean1 = r(mean);
    test treat_supply=treat_boys;
    estadd scalar s1=r(p);
    test treat_supply treat_boys;
    estadd scalar s2=r(p);

    estimates store `out'_active;
	
};

#d;
global balance_varorder ipv_often_bl ipv_12mo_bl ipv_often_active ipv_12mo_active
                        had_sex_bl has_partner_bl partner_past2yr_bl total_partners_bl 
                        momtalk_sex_never_bl age_bl hghst_grade_bl married_coh own_house_bl;

esttab $balance_varorder, se nostar keep(treat_supply treat_boys) stats(outmean1);

matrix C1=r(coefs);
matrix S1=r(stats);


#d;
use "data/girls-replication-data.dta", clear;
keep if endline==0&panel_bal==2;

#d;	  
foreach out in $balance_outcomes {;
	
	
reg `out' treat_goal i.clubs
		if endline==0&panel_bal==2, cluster(clubs);
		
sum `out' if endline==0&treat_goal==0&panel_bal==2;
estadd scalar outmean2 = r(mean);


estimates store `out';
	
};


#d;
use "data/girls-replication-data.dta", clear;
keep if panel_bal==2&had_sex_bl==1&partner_past2yr_bl==1;


#d;
global outcomes ipv_often ipv_12mo;

				  	
#d;			
foreach out in $outcomes {;
	
    reg `out' treat_goal i.clubs
		if endline==0&panel_bal==2, cluster(clubs);
		
sum `out' if endline==0&treat_goal==0&panel_bal==2;
estadd scalar outmean2 = r(mean);


estimates store `out'_active;
	
};


esttab $balance_varorder, se nostar keep(treat_goal) stats(outmean2 );

matrix C2=r(coefs);
matrix S2=r(stats);

matrix C=C1\C2;
matrix S=S1\S2;
 
matrix list C;
matrix list S;

#d;
*Treatment Coefficients;
local rnames : rownames C;
local models : coleq C;
local models : list uniq models;

*Mean and observations;
local mnames : rownames S;


local j 0;
capture matrix drop b;
foreach model of local models {;
    local ++j;
    matrix tmp = S[1, `j'];
    matrix colnames tmp = `model';
    matrix b = nullmat(b), tmp;
};
ereturn post b;
eststo `name';




local i 0;
foreach name of local rnames {;
    local ++i;
    if `i'!=3 {;
        local j 0;
        capture matrix drop b;
        capture matrix drop se;
        foreach model of local models {;
            local ++j;
            matrix tmp = C[`i', 2*`j'-1];
            if tmp[1,1]<. {;
                matrix colnames tmp = `model';
                matrix b = nullmat(b), tmp;
                matrix tmp[1,1] = C[`i', 2*`j'];
                matrix se = nullmat(se), tmp;
            };
        };
        ereturn post b;
        quietly estadd matrix se;
        eststo `name';
    };
};


local j 0;
capture matrix drop b;
foreach model of local models {;
    local ++j;
    matrix tmp = S[2, `j'];
    matrix colnames tmp = `model';
    matrix b = nullmat(b), tmp;
};
ereturn post b;
eststo `name';

local j 0;
capture matrix drop b;
capture matrix drop se;
foreach model of local models {;
    local ++j;
    matrix tmp = C[3, 2*`j'-1];
    if tmp[1,1]<. {;
        matrix colnames tmp = `model';
        matrix b = nullmat(b), tmp;
        matrix tmp[1,1] = C[3, 2*`j'];
        matrix se = nullmat(se), tmp;
    };
};
ereturn post b;
quietly estadd matrix se;
eststo `name';



#d;
esttab using "output/Table_A4.tex", se  
mtitles("ELA Mean" "Boys" "Supply" "No Goal Mean" "Goal")  star(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)))
label style(tex) noconstant replace;

estimates clear;
eststo clear;

#d;
foreach treatment in treat_boys treat_supply treat_goal {;

    probit `treatment' $balance_outcomes if endline==0&panel_bal==2, cluster(clubs);
    test $balance_outcomes;
    estadd scalar chi_p = r(p);
    estimates store `treatment';

};

esttab, se  
 star(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(chi_p)
label style(tex) noconstant replace;

#d;
estimates clear;
eststo clear;


#d;
use "data/girls-replication-data.dta", clear;
keep if endline==0&panel_bal==2;

/* global balance_outcomes ipv_often ipv_12mo 
                        had_sex has_partner partner_past2yr total_partners 
                        momtalk_sex_never age hghst_grade married_coh own_house; */


#d;
file open table_file using "output/table_A4.tex", write append;
qui logit treat_boys $balance_outcomes i.region if endline==0&treat_supply==0, cluster(clubs);
test $balance_outcomes;
file write table_file "p-value for Boys-ELA: " "`r(p)'" _n;
qui logit treat_supply $balance_outcomes i.region if endline==0, cluster(clubs);
test $balance_outcomes;
file write table_file "p-value for supply-ELA: " "`r(p)'" _n;
qui logit treat_goal $balance_outcomes i.clubs if endline==0, cluster(clubs);
test $balance_outcomes;			  
file write table_file "p-value for Goal-No Goal: " "`r(p)'" _n;
file close table_file;


**************************************************
* Table A5: External Validity


/*NOTE: This table uses data from the Demographic and Health Surveys - Tanzania
 2015-16. The data can be accessed from 
 https://dhsprogram.com/methodology/survey/survey-display-485.cfm*/
**************************************************


#d;
gl ev_varnames "ipv_often_p ipv_12mo_p physsex_ipv_often_p physsex_ipv_12mo_p 
				psych_abuseO_p psych_abuse_p sample_size_p
				ipv_often ipv_12mo physsex_ipv_often physsex_ipv_12mo 
				psych_abuse_often psych_abuse sample_size
				age hghst_grade had_sex married sps sample_size_dem";
#delimit cr
use "data/girls-replication-data.dta", clear

gl ipv_outcomes ipv_often ipv_12mo physsex_ipv_often physsex_ipv_12mo psych_abuse_often psych_abuse

gl other_covs age hghst_grade had_sex married_coh sps

sum $ipv_outcomes if endline==0 & panel_bal==2 & age >= 15 & partner_past2yr==1

sum $ipv_outcomes $other_covs if endline==0 & panel_bal==2 & age >= 15


tabstat $ipv_outcomes if endline==0 & panel_bal==2 & age >= 15 & partner_past2yr==1, stat(mean) col(statistics) long save
return list
matrix means = r(StatTotal)

qui count if endline==0 & panel_bal==2 & age >= 15 & partner_past2yr==1
matrix N = r(N)
matrix colnames N = "sample_size"
matrix means = means,N

estpost tabstat $ipv_outcomes if endline==0 & panel_bal==2 & age >= 15 & partner_past2yr==1,  statistics(mean N) columns(statistics) listwise

esttab using "output/Table_A5.tex", main(mean) unstack replace


tabstat $ipv_outcomes $othervars if endline==0 & panel_bal==2 & age >= 15, stat(mean) col(statistics) long save
return list
matrix means = means,r(StatTotal)


qui count if endline==0 & panel_bal==2 & age >= 15
matrix N = r(N)
matrix colnames N = "sample_size"
matrix means = means,N

estpost tabstat $ipv_outcomes $other_covs if endline==0 & panel_bal==2 & age >= 15,  statistics(mean N) columns(statistics) listwise

esttab using "output/Table_A5.tex", main(mean) unstack append 

/*
#delimit cr
use "data/TZIR7AFL.DTA", clear

gen ipv_ever = 0 if d104 ==0 & d106==0 & d107==0 & d108==0
foreach x in d104 d106 d107 d108 {
replace ipv_ever =1 if `x'==1
}

gen weight= d005/1000000
tab ipv_ever [aw=weight] if v012<25

gen jealous1 = 0 if d101a==0
replace jealous1 = 1 if d101a==1

gen jealous = jealous1
replace jealous = 0 if v044==1 & jealous==.

gen insist1 = 0 if d101e ==0
replace insist1 = 1 if d101e==1

gen insist = insist1
replace insist = 0 if v044==1 & insist==.

gen psych_abuse1 =0 if d103a!=.
gen psych_abuse = 0 if v044==1

foreach x in /*d103a*/ d103b d103c {

replace psych_abuse1 = 1 if `x' == 1 | `x'==2
replace psych_abuse = 1 if `x' == 1 | `x'==2
}


gen phys_abuse1 =0 if d103a!=.
gen phys_abuse = 0 if v044==1

foreach x in d105a /*d105b d105c d105d d105e d105f d105j*/ {

replace phys_abuse1 = 1 if `x' == 1 | `x'==2
replace phys_abuse = 1 if `x' == 1 | `x'==2
}

gen force_sex1 =0 if d103a!=.
gen force_sex = 0 if v044==1

foreach x in d105h /*d105i d105k*/ {

replace force_sex1 = 1 if `x' == 1 | `x'==2
replace force_sex = 1 if `x' == 1 | `x'==2
}

gen psych_abuseO1 =0 if d103a!=.
gen psych_abuseO = 0 if v044==1

foreach x in /* d103a*/ d103b d103c {

replace psych_abuseO1 = 1 if `x' == 1 
replace psych_abuseO = 1 if `x' == 1 
}
*

gen phys_abuseO1 =0 if d103a!=.
gen phys_abuseO = 0 if v044==1

foreach x in d105a {

replace phys_abuseO1 = 1 if `x' == 1
replace phys_abuseO = 1 if `x' == 1 
}

gen force_sexO1 =0 if d103a!=.
gen force_sexO = 0 if v044==1

foreach x in d105h {

replace force_sexO1 = 1 if `x' == 1 
replace force_sexO = 1 if `x' == 1
}
gen ipv_12m = 0 if psych_abuse==0 & phys_abuse==0 & force_sex==0
replace ipv_12m = 1 if psych_abuse==1 | phys_abuse==1 | force_sex==1

gen ipv_often = 0 if psych_abuseO==0 & phys_abuseO==0 & force_sexO==0
replace ipv_often = 1 if psych_abuseO==1 | phys_abuseO==1 | force_sexO==1

gen ipv_12m1 = 0 if psych_abuse1==0 & phys_abuse1==0 & force_sex1==0
replace ipv_12m1 = 1 if psych_abuse1==1 | phys_abuse1==1 | force_sex1==1

gen ipv_often1 = 0 if psych_abuseO1==0 & phys_abuseO1==0 & force_sexO1==0
replace ipv_often1 = 1 if psych_abuseO1==1 | phys_abuseO1==1 | force_sexO1==1


*Combine phys_abuse with force_sex:
gen phys_sexualO=0 if phys_abuseO==0 & force_sexO==0
replace phys_sexualO=1 if (phys_abuseO==1 | force_sexO==1)

gen phys_sexual=0 if phys_abuse==0 & force_sex==0
replace phys_sexual=1 if (phys_abuse==1 | force_sex==1)

gen phys_sexualO1=0 if phys_abuseO1==0 & force_sexO1==0
replace phys_sexualO1=1 if (phys_abuseO1==1 | force_sexO1==1)

gen phys_sexual1=0 if phys_abuse1==0 & force_sex1==0
replace phys_sexual1=1 if (phys_abuse1==1 | force_sex1==1)

tabstat ipv_often1 ipv_12m1 phys_sexualO1 phys_sexual1 psych_abuseO1 psych_abuse1 [aw=weight] if v012<25, stat(mean) col(statistics) long save
return list
matrix means = r(StatTotal)

qui count if v012<25 & ipv_often1!=.
matrix N = r(N)
matrix colnames N = "sample_size"
matrix means = means,N


matrix Partnered=r(StatTotal)'
estpost tabstat ipv_often1 ipv_12m1 phys_sexualO1 phys_sexual1 psych_abuseO1 psych_abuse1 [aw=weight] if v012<25,  statistics(mean N) columns(statistics) listwise
esttab using "output/Table_A5.tex", main(mean) unstack append


tabstat ipv_often ipv_12m phys_sexualO phys_sexual psych_abuseO psych_abuse [aw=weight] if v012<25, stat(mean) col(statistics) long save
return list
matrix means = means,r(StatTotal)

qui count if v012<25 & ipv_often!=.
matrix N = r(N)
matrix colnames N = "sample_size"
matrix means = means,N

estpost tabstat ipv_often ipv_12m phys_sexualO phys_sexual psych_abuseO psych_abuse [aw=weight] if v012<25,  statistics(mean N) columns(statistics) listwise

esttab using "output/Table_A5.tex", main(mean) unstack append




********************************************************************
*For the demographics (age, highest grade, had sex, married, sps):**
********************************************************************
drop weight
gen weight= v005/1000000

gen age=v012 

gen hghst_grade=. /*imputing year of schooling*/
replace hghst_grade=2 if s108==0 /* Pre-school */
replace hghst_grade=4.6 if s108==1 & hghst_grade==. /* "Primary" */
replace hghst_grade=7.6 if s108==2 & hghst_grade==. /* "post-primary training" */
replace hghst_grade=10 if s108==5 & hghst_grade==. /* "Secondary 'a' level" */
replace hghst_grade=12 if s108==3 & hghst_grade==. /* "Secondary 'o' level" */
replace hghst_grade=14 if s108==4 & hghst_grade==. /* "Post-secondary 'o' level training" */
replace hghst_grade=16 if s108==7 & hghst_grade==. /* "University" */

gen had_sex0=.
replace had_sex0=1 if v525!=0 & v525!=.
replace had_sex0=0 if v525==0 

gen married=.
replace married=1 if v501==1
replace married=0 if (v501<1 | v501>1)& v501!=.


tempfile id_dhs
save "`id_dhs'"


*For sps, we need HH data:
use "data/TZPR7AFL.DTA", clear

*hhm_under18_score*
gen under_18=0
replace under_18=1 if hv105<=18

egen hhm_under18 = total(under_18), by(hhid)

gen hhm_under18_score = 0 if hhm_under18>=6
replace hhm_under18_score = 2 if hhm_under18==5
replace hhm_under18_score = 5 if hhm_under18==4
replace hhm_under18_score = 11 if hhm_under18==3
replace hhm_under18_score = 14 if hhm_under18==2
replace hhm_under18_score = 17 if hhm_under18==1
replace hhm_under18_score = 28 if hhm_under18==0

*Number of HH members ages 6-18 in school:
gen hhm_age6to18 = 0
replace hhm_age6to18=1 if hv105<=18 & hv105>=6
egen total_hhm_age6to18 = total(hhm_age6to18), by(hhid)

gen hhm_inschool6to18 = 0
replace hhm_inschool6to18=1 if hv110==1 & hhm_age6to18==1
egen total_inschool6to18 = total(hhm_inschool6to18), by(hhid)

gen hhm_allinsch6to18 = 0
replace hhm_allinsch6to18 = 3 if total_hhm_age6to18==total_inschool6to18
replace hhm_allinsch6to18 = 5 if total_hhm_age6to18==0

gen floor_material = 0 if hv213 ==11 | hv213==12
replace floor_material = 6 if hv213 >12

gen roof_material = 0 if hv215==11 | hv215==12 
replace roof_material = 6 if hv215==21 | hv215==22 | hv215==23 | hv215==32 | hv215==33 | hv215==96 |  hv215==31

*does your household have electricity:
gen have_electricity = 0 if hv206==0
replace have_electricity = 9 if hv206==1

*do you have more than one television/casset/tape recorder:
gen tv_score = 0 
replace tv_score = 9 if hv207==0 & hv208==0 /*DHS we are only using whether the hh has tv or radio, no info on number of tv*/
replace tv_score = 15 if hv207==1 | hv208==1

*do you have a refrigerator/sofa;
gen refsofa_score = 0 if hv209==0
replace refsofa_score = 4 if hv209==1

*land and cattle;
gen has_land = 0
replace has_land=1 if hv244==1

gen has_cattle=0
replace has_cattle=1 if hv246a!=0

gen crop_animal = 0 if has_land==0&has_cattle==0
replace crop_animal = 0 if has_land==0 & has_cattle==1
replace crop_animal = 5 if has_land==1 & has_cattle==0
replace crop_animal = 12 if has_land==1 & has_cattle==1

gen sps = hhm_under18_score + hhm_allinsch6to18 + floor_material + roof_material + have_electricity + tv_score + refsofa_score + crop_animal

keep sps hhid hv001 hv002  /*the ids for hh that we merge with female data*/

rename hv001 v001 
rename hv002 v002

*keep one observation per hh as we just need the sps:
sort hhid 
by hhid: keep if _n == 1


tempfile hh_dhs
save "`hh_dhs'"



*Merge with individual data:
use "`id_dhs'", clear

merge m:1 v001 v002 using "`hh_dhs'"
keep if _merge==3
drop _merge


*For all:

tabstat age hghst_grade had_sex0  married  sps [aw=weight] if age<25 & hghst_grade!=., stat(mean) col(statistics) long save
return list


matrix means = means,r(StatTotal)

qui count if age<25 & hghst_grade!=.
matrix N = r(N)
matrix colnames N = "sample_size"
matrix means = means,N

matrix colnames means = $ev_varnames

ereturn post means

eststo dhs_means

estpost tabstat age hghst_grade had_sex0  married  sps [aw=weight] if age<25, statistics(mean N)   columns(statistics) listwise

esttab ela_means dhs_means using "output/Table_A5.tex", se mtitle("ELA" "DHS") replace
eststo clear


esttab using "output/Table_A5.tex", main(mean) unstack append

*/


**************************************************
* Table A6: Attrition: Female Sample
**************************************************


#delimit;
eststo clear;
use "data/girls-replication-data.dta", clear;


keep if endline==0;

foreach x in momtalk_sex_never
						age
						hghst_grade
						married_coh
						own_house
						electricity
						hhm_number
						ipv_12mo 
						prtnr_index {;
	qui sum `x' ;
	replace `x' = `x' - r(mean);
};

gl control momtalk_sex_never
						age
						hghst_grade
						married_coh
						own_house
						electricity
						hhm_number;

#delimit;



*PANEL A. Differential Attrition by Treatment Status, fully interacted;			 
eststo: xi: reg attrit treat_boys_goal treat_supply_goal treat_boys treat_supply treat_goal
				  age_bl i.region, 
				cluster(clubs);
			
	sum attrit if endline==0 & control_group==1;
	estadd scalar m = r(mean);
	
	test treat_supply_goal treat_boys_goal treat_supply treat_boys treat_goal;
	local p=r(p);

	test treat_supply + treat_supply_goal = treat_boys + treat_boys_goal;
	estadd scalar p2=r(p);
			 
esttab, 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 scalars(m p p2 r2 ar2)
			 replace;


#d;
local colnames "boys supply goal";

#delimit;
local i = 1;
foreach column_length of numlist 2 3 {;

	mat col_coefs = r(coefs);

	mat col_coefs = col_coefs[`i'..`=`i'+`column_length'-1', 1..2];

	local names "";
	forvalues j = 1/`column_length' {;
		local pos = `j';
		local word: word `pos' of `colnames';
		local names "`names' `word'";
	};
	matrix rownames col_coefs = `names';

    matrix b = col_coefs[1..`column_length',1];
    matrix se = col_coefs[1..`column_length',2];

	mat b = b';
	mat se = se';

    ereturn post b;
    quietly estadd matrix se;

	eststo col_`i';

    local i = `i' + `column_length';

};

estadd scalar p=`p';

qui describe;
estadd scalar obs=r(N);


#d;
esttab col_* using "output/Table_A6.tex", se mtitle("xGoal" "Levels") scalars(obs p) replace;



#delimit;
eststo clear;
use "data/girls-replication-data.dta", clear;


keep if endline==0;

foreach x in momtalk_sex_never
						age
						hghst_grade
						married_coh
						own_house
						electricity
						hhm_number
						ipv_12mo 
						prtnr_index {;
	qui sum `x' ;
	replace `x' = `x' - r(mean);
};
*PANEL b: Differential Attrition by Treatment Status and Key Outcome;
keep if endline==0;

		
		#d;
foreach var in $control ipv_12mo prtnr_index {;
				
		gen a_mst_`var' = treat_supply*`var';
		gen a_grs_`var' = treat_boys*`var';
		gen a_gs_`var' = treat_goal*`var';
		
		gen a_mstgs_`var'=treat_supply_goal*`var';
		gen a_grsgs_`var'=treat_boys_goal*`var';
				
				} ;

#d;
eststo: xi: areg attrit a_grsgs_*
                        a_mstgs_*
						a_grs_*
						a_mst_*
						a_gs_* treat_boys_goal treat_supply_goal

						$control 
						ipv_12mo
						prtnr_index 
						treat_supply treat_boys treat_goal 
						i.region
						,
						cluster(clubs)
						abs(region);


						
test treat_supply + treat_supply_goal = treat_boys + treat_boys_goal;
	local panel_b_p=r(p);
	
test a_grs_ipv_12mo a_grs_prtnr_index a_grs_momtalk_sex_never a_grs_age 
	 a_grs_hghst_grade a_grs_married_coh a_grs_own_house a_grs_electricity 
	 a_grs_hhm_number;
	local grsp=r(p);
	
test a_gs_ipv_12mo a_gs_prtnr_index a_gs_momtalk_sex_never a_gs_age 
	 a_gs_hghst_grade a_gs_married_coh a_gs_own_house a_gs_electricity 
	 a_gs_hhm_number;
	local gsp=r(p);
	
test a_mst_ipv_12mo a_mst_prtnr_index a_mst_momtalk_sex_never a_mst_age 
	 a_mst_hghst_grade a_mst_married_coh a_mst_own_house a_mst_electricity 
	 a_mst_hhm_number;
	local mstp=r(p);
	
test a_grsgs_ipv_12mo a_grsgs_prtnr_index a_grsgs_momtalk_sex_never 
	 a_grsgs_age a_grsgs_hghst_grade a_grsgs_married_coh a_grsgs_own_house 
	 a_grsgs_electricity a_grsgs_hhm_number;
	local grsgsp=r(p);
	
test a_mstgs_ipv_12mo a_mstgs_prtnr_index a_mstgs_momtalk_sex_never 
	 a_mstgs_age a_mstgs_hghst_grade a_mstgs_married_coh a_mstgs_own_house 
	 a_mstgs_electricity a_mstgs_hhm_number;
	local mstgsp=r(p);
	
	#d;
test a_grs_ipv_12mo + a_grs_prtnr_index + a_grs_momtalk_sex_never + a_grs_age + 
	 a_grs_hghst_grade + a_grs_married_coh + a_grs_own_house + a_grs_electricity + 
	 a_grs_hhm_number + a_grsgs_ipv_12mo + a_grsgs_prtnr_index + a_grsgs_momtalk_sex_never + 
	 a_grsgs_age + a_grsgs_hghst_grade + a_grsgs_married_coh + a_grsgs_own_house +
	 a_grsgs_electricity + a_grsgs_hhm_number
	 =
	 a_mst_ipv_12mo + a_mst_prtnr_index + a_mst_momtalk_sex_never + a_mst_age +
	 a_mst_hghst_grade + a_mst_married_coh + a_mst_own_house + a_mst_electricity + 
	 a_mst_hhm_number + a_mstgs_ipv_12mo + a_mstgs_prtnr_index + a_mstgs_momtalk_sex_never +
	 a_mstgs_age + a_mstgs_hghst_grade + a_mstgs_married_coh + a_mstgs_own_house +
	 a_mstgs_electricity + a_mstgs_hhm_number;
	local mstgrs=r(p);

esttab, se 
		star(* 0.10 ** 0.05 *** 0.01)
		replace;



#d;
local colnames "Never_talks_to_mother_about_srh
				Age_in_years
				Highest_grade_attended
				Married_or_cohabiting
				Owns_a_house
				Has_electricity
				Number_of_household_members
				IPV_in_past_year
				Sexual_activity_index
				boys supply goal";

local locals_list "grsgsp mstgsp grsp mstp gsp lastcol";

#delimit;
local step = 1;
local i = 1;
foreach column_length of numlist 9 9 9 9 11 12 {;

	mat col_coefs = r(coefs);

	mat col_coefs = col_coefs[`i'..`=`i'+`column_length'-1', 1..2];

	local names "";
	forvalues j = 1/`column_length' {;
		local pos = `j';
		local word: word `pos' of `colnames';
		local names "`names' `word'";
	};
	matrix rownames col_coefs = `names';

    matrix b = col_coefs[1..`column_length',1];
    matrix se = col_coefs[1..`column_length',2];

	mat b = b';
	mat se = se';

    ereturn post b;
    quietly estadd matrix se;

	local word : word `step' of `locals_list';
	di "`word'";
	if "`word'" == "lastcol" local p = .;
	else if "`word'" == "grsp" local p = `grsp';
	else if "`word'" == "gsp" local p = `gsp';
	else if "`word'" == "mstp" local p = `mstp';
	else if "`word'" == "grsgsp" local p = `grsgsp';
	else if "`word'" == "mstgsp" local p = `mstgsp';
	else if "`word'" == "mstgrs" local p = `mstgrs';

	estadd scalar p_fullrow=`p';

	eststo col_`i';

    local i = `i' + `column_length';
	local step = `step' + 1;
};


estadd scalar panel_b_p=`panel_b_p';
estadd scalar grsp=`grsp';
estadd scalar gsp=`gsp';
estadd scalar mstp=`mstp';
estadd scalar grsgsp=`grsgsp';
estadd scalar mstgsp=`mstgsp';
estadd scalar mstgrs=`mstgrs';

qui describe;
estadd scalar obs=r(N);

					
esttab col_* using "output/Table_A6.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 scalars(obs p_fullrow)
			 mtitle("×Boys×Goal" "×Supply×Goal" "×Boys" "×Supply" "×Goal" "Levels")
			 append;

estimates clear;
eststo clear;			 


**************************************************
* Table A7: Attrition: Boys Sample
**************************************************

#delimit;
eststo clear;

use "data/boys-replication-data.dta", replace;

keep if endline==0;
	
gen attrit = 0 if panel_bal==2;
replace attrit=1 if panel_bal==1;

	local i=1;
foreach x in hghst_grade {;
			 
			 gen temp`i' = `x'==.;
			 rename `x' `x'1;
			 gen `x'=`x'1;
			 replace `x' = 0 if `x'1==.;
			 
	local i=`i'+1	;	 
			 };


foreach x in viol_indexbg knowlindex partner_indexbg 
			 dadtalk_sex_never age hghst_grade married_coh electricity 
				hhm_number {;
	qui sum `x';
	replace `x' = `x' - r(mean);
};

gl control dadtalk_sex_never
						age
						hghst_grade
						married_coh
						own_house
						electricity
						hhm_number;

#delimit;


*PANEL A. Differential Attrition by Treatment Status, fully interacted;			 
eststo: xi: reg attrit treat_boys_goal treat_supply_goal treat_boys treat_supply treat_goal i.region, 
				cluster(girlid);
			
	sum attrit if endline==0 & control_group==1;
	estadd scalar m = r(mean);
	
	test treat_supply_goal treat_boys_goal treat_supply treat_boys treat_goal;
	local p=r(p);

	test treat_supply + treat_supply_goal = treat_boys + treat_boys_goal;
	estadd scalar p2=r(p);
			 
esttab, 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 scalars(m p p2 r2 ar2)
			 replace;


#d;
local colnames "boys supply goal";

#delimit;
local i = 1;
foreach column_length of numlist 2 3 {;

	mat col_coefs = r(coefs);

	mat col_coefs = col_coefs[`i'..`=`i'+`column_length'-1', 1..2];

	local names "";
	forvalues j = 1/`column_length' {;
		local pos = `j';
		local word: word `pos' of `colnames';
		local names "`names' `word'";
	};
	matrix rownames col_coefs = `names';

    matrix b = col_coefs[1..`column_length',1];
    matrix se = col_coefs[1..`column_length',2];

	mat b = b';
	mat se = se';

    ereturn post b;
    quietly estadd matrix se;

	eststo col_`i';

    local i = `i' + `column_length';

};

estadd scalar p=`p';

qui describe;
estadd scalar obs=r(N);


#d;
esttab col_* using "output/Table_A7.tex", se mtitle("xGoal" "Levels") scalars(obs p) replace;



#delimit;
eststo clear;
use "data/boys-replication-data.dta", replace;

keep if endline==0;
	
gen attrit = 0 if panel_bal==2;
replace attrit=1 if panel_bal==1;

	local i=1;
foreach x in hghst_grade {;
			 
			 gen temp`i' = `x'==.;
			 rename `x' `x'1;
			 gen `x'=`x'1;
			 replace `x' = 0 if `x'1==.;
			 
	local i=`i'+1	;	 
			 };


foreach x in viol_indexbg knowlindex partner_indexbg 
			 dadtalk_sex_never age hghst_grade married_coh electricity 
				hhm_number {;
	qui sum `x';
	replace `x' = `x' - r(mean);
};
*PANEL b: Differential Attrition by Treatment Status and Key Outcome;

		
		#d;
foreach var in $control knowlindex partner_indexbg viol_indexbg {;
				
		gen a_mst_`var' = treat_supply*`var';
		gen a_grs_`var' = treat_boys*`var';
		gen a_gs_`var' = treat_goal*`var';
		
		gen a_mstgs_`var'=treat_supply_goal*`var';
		gen a_grsgs_`var'=treat_boys_goal*`var';
				
				} ;

#d;
eststo: xi: areg attrit a_grsgs_*
                        a_mstgs_*
						a_grs_*
						a_mst_*
						a_gs_* treat_boys_goal treat_supply_goal

						$control 
						knowlindex partner_indexbg viol_indexbg
						treat_supply treat_boys treat_goal 
						temp1
						c.temp1#treat_supply
						c.temp1#treat_supply_goal
						c.temp1#treat_boys
						c.temp1#treat_boys_goal
						c.temp1#treat_goal
						,
						cluster(girlid)
						abs(region);


						
		
test treat_supply + treat_supply_goal = treat_boys + treat_boys_goal;
	local panel_b_p=r(p);
	
test a_grs_viol_indexbg a_grs_knowlindex a_grs_partner_indexbg 
	 a_grs_dadtalk_sex_never a_grs_age a_grs_hghst_grade a_grs_married_coh 
	 a_grs_electricity a_grs_hhm_number;
	local grsp=r(p);
	
test a_gs_viol_indexbg a_gs_knowlindex a_gs_partner_indexbg 
	 a_gs_dadtalk_sex_never a_gs_age a_gs_hghst_grade a_gs_married_coh 
	 a_gs_electricity a_gs_hhm_number;
	local gsp=r(p);
	
test a_mst_viol_indexbg a_mst_knowlindex a_mst_partner_indexbg 
	 a_mst_dadtalk_sex_never a_mst_age a_mst_hghst_grade a_mst_married_coh 
	 a_mst_electricity a_mst_hhm_number;
	local mstp=r(p);
	
test a_grsgs_viol_indexbg a_grsgs_knowlindex a_grsgs_partner_indexbg 
	 a_grsgs_dadtalk_sex_never a_grsgs_age a_grsgs_hghst_grade 
	 a_grsgs_married_coh a_grsgs_electricity a_grsgs_hhm_number;
	local grsgsp=r(p);
	
test a_mstgs_viol_indexbg a_mstgs_knowlindex a_mstgs_partner_indexbg 
	 a_mstgs_dadtalk_sex_never a_mstgs_age a_mstgs_hghst_grade 
	 a_mstgs_married_coh a_mstgs_electricity a_mstgs_hhm_number;
	local mstgsp=r(p);
	
	#d;
test a_grs_viol_indexbg + a_grs_knowlindex + a_grs_partner_indexbg + 
	 a_grs_dadtalk_sex_never + a_grs_age + a_grs_hghst_grade + a_grs_married_coh + 
	 a_grs_electricity + a_grs_hhm_number + a_grsgs_viol_indexbg + a_grsgs_knowlindex + 
	 a_grsgs_partner_indexbg + 
	 a_grsgs_dadtalk_sex_never + a_grsgs_age + a_grsgs_hghst_grade + 
	 a_grsgs_married_coh + a_grsgs_electricity + a_grsgs_hhm_number
	 =
	 a_mst_viol_indexbg + a_mst_knowlindex + a_mst_partner_indexbg  +
	 a_mst_dadtalk_sex_never + a_mst_age + a_mst_hghst_grade + a_mst_married_coh + 
	 a_mst_electricity + a_mst_hhm_number + a_mstgs_viol_indexbg + a_mstgs_knowlindex + 
	 a_mstgs_partner_indexbg + 
	 a_mstgs_dadtalk_sex_never + a_mstgs_age + a_mstgs_hghst_grade + 
	 a_mstgs_married_coh + a_mstgs_electricity + a_mstgs_hhm_number;
	local mstgrs=r(p);

local locals_list "grsgsp mstgsp grsp mstp gsp lastcol";


esttab, se 
		star(* 0.10 ** 0.05 *** 0.01)
		replace;



#d;
local colnames "Never_talks_to_father_about_srh
				Age_in_years
				Highest_grade_attended
				Married_or_cohabiting
				Owns_a_house
				Has_electricity
				Number_of_household_members
				Risk_Perception_Index
				Sexual_activity_index
				Violence_Index
				boys supply goal";

#delimit;
local i = 1;
local step = 1;
foreach column_length of numlist 10 10 10 10 12 13 {;

	mat col_coefs = r(coefs);

	mat col_coefs = col_coefs[`i'..`=`i'+`column_length'-1', 1..2];

	local names "";
	forvalues j = 1/`column_length' {;
		local pos = `j';
		local word: word `pos' of `colnames';
		local names "`names' `word'";
	};
	matrix rownames col_coefs = `names';

    matrix b = col_coefs[1..`column_length',1];
    matrix se = col_coefs[1..`column_length',2];

	mat b = b';
	mat se = se';

    ereturn post b;
    quietly estadd matrix se;

	local word : word `step' of `locals_list';
	di "`word'";
	if "`word'" == "lastcol" local p = .;
	else if "`word'" == "grsp" local p = `grsp';
	else if "`word'" == "gsp" local p = `gsp';
	else if "`word'" == "mstp" local p = `mstp';
	else if "`word'" == "grsgsp" local p = `grsgsp';
	else if "`word'" == "mstgsp" local p = `mstgsp';
	else if "`word'" == "mstgrs" local p = `mstgrs';

	estadd scalar p_fullrow=`p';

	eststo col_`i';

	

    local i = `i' + `column_length';
	local step = `step' + 1;
};


estadd scalar panel_b_p=`panel_b_p';
estadd scalar grsp=`grsp';
estadd scalar gsp=`gsp';
estadd scalar mstp=`mstp';
estadd scalar grsgsp=`grsgsp';
estadd scalar mstgsp=`mstgsp';
estadd scalar mstgrs=`mstgrs';

qui describe;
estadd scalar obs=r(N);

#d;				
esttab col_* using "output/Table_A7.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 scalars(p_fullrow)
			 mtitle("×Boys×Goal" "×Supply×Goal" "×Boys" "×Supply" "×Goal" "Levels")
			 append;

estimates clear;
eststo clear;


**************************************************
* Table A8: Impact of Treatment, No Controls
**************************************************




#d;
local sample_num = 1;
foreach sample in ""
				 " & partner_past2yr_bl==1 & had_sex_bl==1"
                 " & (grs_anyboy_enr==1 | treat_boys==0)"
                  {;

    #delimit;
    use "data/girls-replication-data.dta", clear;

    keep if panel_bal==2&endline==1 `sample';

    #d ;
    foreach x in $controls_bl {;
        qui sum `x' ;
        replace `x' = `x' - r(mean);
    };

    foreach out in $ipv3b {;
        areg `out' treat_boys treat_goal treat_supply 
                treat_boys_goal treat_supply_goal 
                `out'_bl, cluster(clubs) abs(region);
        
        if `sample_num' == 3 {;
            sum `out' if control_group==1&treat_goal==0&nameboysex_bl==1 ;
            estadd scalar outmean = r(mean);
            estadd scalar outsd = r(sd);
        };
        else {;
            sum `out' if control_group==1&treat_goal==0 ;
            estadd scalar outmean = r(mean);
            estadd scalar outsd = r(sd);
        };

        estimates store `out'_`sample_num';
    };

    local ++sample_num;
};

*Transposed Table;
esttab ipv_often_1 ipv_12mo_1 ipv_often_2 ipv_12mo_2 ipv_often_3 ipv_12mo_3, se keep (treat_boys treat_goal treat_supply 
	treat_boys_goal treat_supply_goal)
	stats(outmean N);

*Matrix Extraction;
matrix c =r(coefs);
matrix S =r(stats);

mat list c;

matrix mean0 = S[1,1..6]; /*Stats matrix of means*/;
matrix N = S[2,1..6]; /*sample size*/;
matrix mean1 = mean0\N;

label variable ipv_12mo "IPV in past year";
label variable ipv_often "IPV often";


*FIRST table for the "x Post";
local rnames: rownames c;
local models: coleq c;
local models: list uniq models;

*The treatment coefficients;
local i 0;
foreach name of local rnames {;
    local ++i;
    local j 0;
    capture matrix drop b;
    capture matrix drop se;
    capture matrix drop p;
    foreach model of local models {;
        local ++j;
        matrix tmp = c[`i',3*`j'-2];
        if tmp[1,1]<. {;
        matrix colnames tmp = `model';
        matrix b=nullmat(b), tmp;
        matrix tmp[1,1]=c[`i',3*`j'-1];
        matrix se = nullmat(se), tmp;
        matrix tmp[1,1]=c[`i',3*`j'];
        matrix p = nullmat(p), tmp;
    };
};
ereturn post b;
quietly estadd matrix se;
quietly estadd matrix p;

*mat li p;
mat li se;

eststo `name';
};

*Mean and observations;
local mnames : rownames mean1;
local i 0;
foreach name of local mnames {;
local ++i;
local j 0;
capture matrix drop b;
foreach model of local models {;
local ++j;
matrix tmp = mean1[`i', `j'];
matrix colnames tmp = `model';
matrix b = nullmat(b), tmp;
};
ereturn post b;
eststo `name';
};

#d;
esttab using "output/Table_A8.tex", se  
mtitles("Boys" "Goal" "Supply" "BoysxGoal" "SupplyxGoal"
		"Mean" "Observations")  
		title("Impact of Treatments on Intimate Partner Violence (IPV)") 
star(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3)))
label style(tex) noconstant noobs replace;

eststo clear;

**************************************************
* Table D1: Endline Characteristics by Baseline Partnership
**************************************************

#d;
use "data/girls-partners-replication-data.dta", clear;

gl outcomes age hghst_grade enrolled married_coh own_house hhm_number ipv_often ipv_12mo;


keep if p_listed==1 & endline==1;
				 
#delimit;
tabstat $outcomes if old==0, stats(mean) col(statistics) long save;

matrix means = r(StatTotal);
qui count if old==0;
matrix N = r(N);
matrix colnames N = "sample_size";
matrix means = means,N;

ereturn post means;
eststo not_with_baseline_partner;

tabstat $outcomes if old==1, stats(mean) columns(statistics) long save;

matrix means = r(StatTotal);
qui count if old==1;
matrix N = r(N);
matrix colnames N = "sample_size";
matrix means = means,N;

ereturn post means;
eststo with_baseline_partner;

foreach out in $outcomes {;
	reg `out' old, robust;
    matrix tmp = 2*ttail(e(df_r),abs(_b[old]/_se[old]));
	mat p_values = nullmat(p_values), tmp;
};

matrix colnames p_values = $outcomes;
ereturn post p_values;
eststo p_values;

esttab not_with_baseline_partner with_baseline_partner p_values using "output/Table_D1.tex", mtitle("Not with baseline partner" "With baseline partner" "p-values") replace;
eststo clear;
